<!doctype html>
<html lang="en">
<head>
  <meta charset="UTF-8">
  <meta name="viewport"
        content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
  <meta http-equiv="X-UA-Compatible" content="ie=edge">
  <title>逻辑运算符短路特性</title>
</head>
<body>
  <form id="form" action="https://github.com/miracle-git">
    <label for="username">
      用户名：<input type="text" name="username" id="username">
    </label>
    <label for="term">
      <input type="checkbox" name="term" id="term"> 接受协议
    </label>
    <hr>
    <button>提交</button>
  </form>
  <script>
    function query(el) {
      return document.querySelector(el)
    }
    query('#form').addEventListener('submit', function (e) {
      const username = query('#username').value.trim()
      const checked = query('#term').checked
      if (!username || !checked) {
        alert('请填写用户名同时接受协议')
        e.preventDefault()
      }
    })
  </script>
</body>
</html>